﻿#region File Header

/* File Name    : FacebookApplicationManager.cs
 * Description  : Contains methods to retrieve application details
 * Log
 * Date             Author              Description
 * 31-Oct-2011      Robin Jome          Class is created
 */

#endregion

using System.Collections.Generic;
using FacebookAPI.Base;
using FacebookAPI.FacebookProperties;
using FacebookAPI.Objects;

namespace FacebookAPI.Managers
{
    public class FacebookApplicationManager
    {
        public FacebookConnection<FacebookApplication> GetAllApplications(string facebookObjectId, string fields, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            FacebookQueryField queryfield = null;
            if (!string.IsNullOrEmpty(fields))
            {
                queryfield = new FacebookQueryField()
                {
                    FieldName = "accounts",
                    FieldValues = new List<string>() { fields }
                };
            }
            return mgr.GetConnectionObject<FacebookApplication>(mgr.GetUrl(facebookObjectId, FacebookConnections.Accounts, queryfield, null, accessToken, false));
        }

        public FacebookApplication GetApplicationDetails(string applicationId, string fields, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            FacebookQueryField queryField = null;
            if (!string.IsNullOrEmpty(fields))
            {
                queryField = new FacebookQueryField()
                {
                    FieldName = "fields",
                    FieldValues = new List<string>() { fields }
                };
            }
            return mgr.GetObject<FacebookApplication>(mgr.GetUrl(applicationId, null, queryField, null, accessToken, false));
        }

        public string GetAppAccessToken(string appId, string clientSecret)
        {
            FacebookManager mgr = new FacebookManager();
            string response = mgr.GetResponse(mgr.GetUrl("oauth/access_token", null, null, null, null,
                new List<string>() { 
                    "client_id=" + appId, 
                    "client_secret=" + clientSecret,
                    "grant_type=client_credentials"
                }, false));

            return ((!string.IsNullOrEmpty(response)) ? response.Replace("access_token=", string.Empty) : string.Empty);
        }
    }
}
